<% layout('/layouts/default.html', {title: '生产工单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stProductBill.isNewRecord ? '新增生产工单' : '编辑生产工单')}
				bug : 审核2件套接收的参数过多;
				预混载体 select 没有禁用;
				批次数更改后行数不动了;
			</div>
			<div class="box-tools pull-right">
				<% if (hasPermi('st:stProductBill:edit')){ %>
				<a href="#" class="btn btn-default" id="btn-src" ><i class="fa fa-filter"></i> ${text('加载销售单')}
				</a>
				<div class="hide">
					<#form:listselect id="btn-src-0" title="销售单选择"

					url="${ctx}/st/stSaleBill/detail" allowClear="false"
					checkbox="false"  callbackFuncName="selectCallback" checkFuncName="check"/>
				</div>
				<a href="#" class="btn btn-default" id="btn-approve" ><i class="fa fa-filter"></i> ${text('审核')}
				</a>
				<a href="#" class="btn btn-default" id="btn-revocation" ><i class="fa fa-filter"></i> ${text('弃审')}
				</a>
				<a href="#" class="btn btn-default" id="btn-save" ><i class="fa fa-filter"></i> ${text('保存')}
				</a>
				<% } %>
				<script>

					function check(id,selectData){
						// console.info("check");
						// 检查生产配方是否一样
						return true;
					}
					function selectCallback(id, act, index, layero, selectData) {
						console.info(selectData);
						var allN=0;
						for (var index in selectData){
							var item = selectData[index];
							allN += item.number;
						}
						var productId = selectData[0].productName;
						var formulaId = selectData[0].productFormula;
						$("#productName").val(productId).trigger('change');
						productSelect(productId, formulaId);
						$("#billNumber").val(allN);
					}
					var aStatus=${stProductBill.approveState!"null"};
					approveBtnStatus(aStatus);
					$("#btn-src").click(function () {
						// $("#btn-src-0Div").attr("data-url", "${ctx}");
						// console.info("url", $("#btn-srcDiv").data("url"));
						$("#btn-src-0Button").click();
					});
					$("#btn-save").click(function () {
						$("#approveState").val(2);
						$("form").submit();
					});
					$("#btn-approve").click(function () {
						if(null == aStatus){
							js.showMessage("还没保存呢");
							return;
						}
						$("#approveState").val(4);
						$("form").submit();
					});
					$("#btn-revocation").click(function () {
						$("#approveState").val(2);
						$("form").submit();
					});
					function refresh(id) {
						location.replace("${ctx}/st/stProductBill/form?id="+id);
						// location.reload();
					}
					function approveBtnStatus(status) {
						var $save=$("#btn-save");
						var $approve=$("#btn-approve");
						var $revocation=$("#btn-revocation");
						if (null == status ) {
							$save.show();
							$approve.hide();
							$revocation.hide();
						}
						if (2 == status) {
							$save.show();
							$approve.show();
							$revocation.hide();
						}
						if (4 == status) {
							$save.hide();
							$approve.hide();
							$revocation.show();
						}
					}
				</script>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<style>
			.detail-del{
				/*display: none;*/
			}
		</style>
		<script src="https://cdn.bootcdn.net/ajax/libs/bignumber.js/9.0.1/bignumber.js"></script>
		<#form:form id="inputForm" model="${stProductBill}" action="${ctx}/st/stProductBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('单据号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('已生产数量')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="hasProductNumber" maxlength="8" class="form-control digits"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('是否研发')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="research" dictType="st_is_research" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('工单状态')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:select path="billStatus" dictType="st_product_bill_status" blankOption="true" class="form-control digits" />-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('计划完成日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="plainCompleteDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('制作要求')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="makeRequest" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('产品名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="productName" items="${productRecordList}"  itemLabel="productName" itemValue="id" blankOption="true" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('本次配方')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="thisFormula" items="${listFormulaName}"  itemLabel="code" itemValue="id" blankOption="true" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('工单类别')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="billType" dictType="st_product_bill_type" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('工单数量')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billNumber" maxlength="8" class="form-control digits required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('每批重量')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="perWeight" maxlength="8" class="form-control digits required" defaultValue="0"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('生产批次')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="productBatch" maxlength="4" class="form-control digits requireds" readonly="true"/>
							</div>
						</div>
					</div>
					<script>
						function calc_batch_number() {
							var n1 = new BigNumber($("#billNumber").val());
							var n2 = new BigNumber($("#perWeight").val());
							var res = n1.div(n2).toNumber();
							$("#productBatch").val(res);
						}

						$("#billNumber").bind("input propertychange", function () {
							calc_batch_number();
						});
						$("#perWeight").bind("input propertychange", function () {
							if(!this.value) return;
							calc_batch_number();
							var perWeight=this.value;
							var $weight = $("[my-index]");
							$.each($weight,function (i, item) {
								var $tr = $(item).closest("tr");
								var status = $tr.find("[del-status]").attr("del-status");
								if (0 == status) {
									var percent = $tr.find(".percent").val();
									var n1 = new BigNumber(percent);
									var n2 = new BigNumber(perWeight);
									this.value = n1.times(n2).div(100).toNumber();
								}
							})
							p_w = perWeight;
						});

					</script>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('尾料上限')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="tailingMaterialUpperLimit" maxlength="8" class="form-control digits"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('尾料下限')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="tailingMaterialLowerLimit" maxlength="8" class="form-control digits"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('预混载体')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="prepareMixCarrier" items="${formulaMaterial}" itemLabel="materialName" itemValue="id" blankOption="true" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('预混桶重量')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="prepareMixWeight" maxlength="8" class="form-control digits" defaultValue="20"/>
								<a href="#" class="btn btn-default" id="btn-pre-mix" title="${text('确定预混')}"><i class="fa fa-filter"></i> ${text('确定预混')}</a>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('回机料名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="backMaterialName" items="${materialRecordListBean}"  itemLabel="materialName" itemValue="id" blankOption="true" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('回机料重量')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="backMaterialWeight" maxlength="8" class="form-control digits" defaultValue="6"/>
								<a href="#" class="btn btn-default" id="btn-back-material" title="${text('确定回机')}"><i class="fa fa-filter"></i> ${text('确定回机')}</a>

								<script>
									$("#prepareMixWeight").bind("input propertychange", function () {
										$("#prepareMixCarrier").val('').trigger('change');
									});
									var pre_mix=0;//是否预混
									$("#btn-back-material").click(function () {
										//把回机料插进去
										$("[del-status=2]").click();
										$('#stProductBillDetailDataGrid').jqGrid('addRow', { position: 'last', // 插入位置：first、last、before、after
											addRowParams: {keys: false, focusField: true},
											initdata: {id: '', status: Global.STATUS_NORMAL}
										});
										var materialName = $(($(($("#stProductBillDetailDataGrid").find("tr"))[1 + t_size_1 + formula_list.length + pre_mix]).find('td'))[3]);
										var materialId = $("#backMaterialName").val();
										materialName.find(".form-control").val(materialId).trigger('change');
										materialName.find("input:hidden").val(materialId);

										var percent = materialName.next();
										percent.find(".form-control").val(0);

										var bmw=$("#backMaterialWeight").val();
										var weigh = percent.next();//重量
										weigh.find(".form-control").val(bmw);

										var productType = weigh.next();
										productType.find(".form-control").val("其他");

										var weighPlace = productType.next();
										weighPlace.find(".form-control").val(1).trigger('change');//todo 不能写死

										var weighOrder = weighPlace.next();
										var op=weighOrder.next()//
												.next()//
												.next()//操作
										op.find("a").attr("del-status","2");
									});
									var insertBeforeIndexWidthPre=0;//预混料插入点
									$("#btn-pre-mix").click(function () {
										//确定预混
										var p_mix = $("#prepareMixWeight").val();
										var carrier=$("#prepareMixCarrier").val();
										if(""== carrier){
											js.showMessage("请选择预混载体");
											return;
										}
										if ("" == p_mix.trim()) {
											js.showMessage("预混桶重量");
											return;
										}
										$("[del-status=1]").click();
										var total = 0;
										var overIndex = 0;//溢出索引
										var totalArr = [];
										for (var i = 0; i < formula_list.length; i++) {
											var item = formula_list[i];
											var n1 = new BigNumber(total);
											var n2 = new BigNumber(item.percentIn);
											var n3 = new BigNumber(p_w);
											total=n1.plus(n2.times(n3).div(100)).toNumber();
											totalArr.push(total);
											if (total >= parseFloat(p_mix)) {
												overIndex = i;
												break;
											}
										}
										insertBeforeIndexWidthPre = overIndex;
										//得到溢出值
										if (total == p_mix) {
											//todo 正好的话该怎么处理
										}
										var n1 = new BigNumber(totalArr[overIndex-1]);
										var n2 = new BigNumber(p_mix);
										var insertNumber = n2.minus(n1).toNumber();
										// console.info('insert', insertNumber);
										var $splitWeight = $("[material-id="+carrier+"]").closest("tr").find(".weight");
										var prime = $splitWeight.val();
										var left = new BigNumber(prime).minus(new BigNumber(insertNumber)).toNumber();
										$splitWeight.val(left);
										//查找到对应的行
										var $insert = $(($("#stProductBillDetailDataGrid").find("tr"))[1+t_size_1+overIndex]);
										var beforeId=$insert.attr("id");
										$('#stProductBillDetailDataGrid').jqGrid('addRow', { position: 'before', // 插入位置：first、last、before、after
											src: beforeId, // 指定行的id，插入到指定位置前或后
											addRowParams: {keys: false, focusField: true},
											initdata: {id: '', status: Global.STATUS_NORMAL}
										});
										var materialName = $(($(($("#stProductBillDetailDataGrid").find("tr"))[1+t_size_1+overIndex]).find('td'))[3]);
										materialName.find(".form-control").val(carrier).trigger('change');
										materialName.find("input:hidden").val(carrier);

										var percent = materialName.next();
										percent.find(".form-control").val(0);

										var weigh = percent.next();//重量
										weigh.find(".form-control").val(insertNumber);

										var productType = weigh.next();
										productType.find(".form-control").val("预混桶");

										var weighPlace = productType.next();
										var $select = weighPlace.find(".form-control").val(1).trigger('change');
										var s = $select.attr("my-sort");//小料间
										$select.attr("my-sort",new BigNumber(s).minus(new BigNumber("0.5")).toNumber())
										var $allSort = $("[my-sort]").filter(function () {
											var sort = parseFloat($(this).attr("my-sort"));
											return sort > t_size_1 && sort <= t_size_1 + overIndex;
										});
										//后面的设置为其他
										var $allSort_2 = $("[my-sort]").filter(function () {
											var f = $(this).attr("my-sort");
											if(f.indexOf(".")!=-1) return false;
											var sort = parseFloat(f);
											return sort > t_size_1 + overIndex;
										});
										$allSort.val(1).trigger('change');


										//插入后的一行都变其他
										$.each($allSort_2,function (i, item) {
											$(item).closest("td").prev().find(".form-control").val("其他");
										})
										//前一格变预混桶
										$.each($allSort,function (i, item) {
											$(item).closest("td").prev().find(".form-control").val("预混桶");
										})
										// console.info($allSort);
										// console.info($allSort_2);
										//
										var weighOrder = weighPlace.next();
										var op=weighOrder.next()//
												.next()//
												.next()//操作
										op.find("a").attr("del-status","1");
										pre_mix=1;//成功预混
									});
								</script>
							</div>
						</div>
					</div>
					<#form:hidden path="approveState"/>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批状态')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:select path="approveState" dictType="st_approve_state" blankOption="true" class="form-control digits" />-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveManName" maxlength="5" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
				</div>
				<h4 class="form-unit">${text('生产工单明细')}</h4>
				<div class="ml10 mr10">
					<table id="stProductBillDetailDataGrid"></table>
					<% if (hasPermi('st:stProductBill:edit')){ %>
					<a href="#" id="stProductBillDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10" style="display: none"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
				<script>
					var t_size_1=0;
					var formula_list=null;
					var p_w = 0;

					function prepareDetail(list,formulaId) {
						$.ajax({
							url: "${ctx}/st/stProductBill/formulaMaterial",
							dataType: 'json',
							type: 'POST',
							data: {
								//请求参数
								formulaNameId: formulaId || list[0].id,
							},
							success: function (result) {
								// console.info("result", result);
								$("#prepareMixCarrier").empty();
								var list = result.list;

								var op = '<option value="">&nbsp;</option>';
								$("#prepareMixCarrier").append(op);
								for (var i = 0; i < list.length; i++) {
									var item = list[i];
									var op = '<option value="' + item.id + '">' + item.materialName + '</option>';
									$("#prepareMixCarrier").append(op);
								}
								$("#prepareMixCarrier").change();
							}
						});
						$.ajax({
							url: "${ctx}/st/formula/stFormulaName/detail",
							dataType: 'json',
							type: 'POST',
							data: {
								//请求参数
								id: formulaId || list[0].id,
							},
							success: function (result) {
								// $('#stProductBillDetailDataGrid').dataGrid('delRowData',''+obj.rowId+'');
								// $('#stProductBillDetailDataGrid').dataGrid('setParam', {data:[], page: 1, rowNum: 5000}, true);
								// $('#stProductBillDetailDataGrid').dataGrid('refresh');
								var $detail = $(".detail-del");

								if (0 == t_size_1 && '' != $("#id").val()) {
									t_size_1 += $detail.size();
								}
								$detail.click();
								var list = result.list;
								formula_list = list;
								// console.info(list);
								p_w = $("#perWeight").val();
								// if("" == p_w){
								// 	js.showMessage("请填写每批重量");
								// 	return;
								// }
								for (var i = 0; i < list.length; i++) {
									$("#stProductBillDetailDataGridAddRowBtn").click();
									var item = list[i];

									var materialName = $(($(($("#stProductBillDetailDataGrid").find("tr"))[i + 1 + t_size_1]).find('td'))[3]);//前面3行都什么啊
									materialName.find(".form-control").val(item.materialName).trigger('change');
									materialName.find("input:hidden").val(item.materialName).attr("material-id", item.materialName);
									//占比
									var percent = materialName.next();
									percent.find(".form-control").val(item.percentIn);

									var n1 = new BigNumber(p_w);
									var n2 = new BigNumber(item.percentIn);
									var res = n1.times(n2).div(100).toNumber();
									var weigh = percent.next();//重量
									weigh.find(".form-control").val(res);

									var productType = weigh.next();
									// productType.find(".form-control").val("预混桶");//

									var weighPlace = productType.next();
									weighPlace.find(".form-control").val(item.materialRecord.weighPlace).trigger('change');
								}
							}
						});
					}

					function productSelect(productId,formulaId) {
						$.ajax({
							url: "${ctx}/st/formula/stFormulaName/id",
							dataType: 'json',
							type: 'POST',
							data: {
								//请求参数
								productId: productId,
							},
							success: function (result) {
								$("#thisFormula").empty();
								var list = result.list;
								for (var i = 0; i < list.length; i++) {
									var item = list[i];
									var op = '<option value="' + item.id + '">' + item.code + '</option>';
									$("#thisFormula").append(op);
								}
								$("#thisFormula").change();
								if(formulaId) $("#thisFormula").val(formulaId).trigger('change');

								prepareDetail(list,formulaId);
							}
						});
					}

					$("#productName").on("select2:select",function(e) {
						var data = e.params.data;
						if(""== data.id) return;
						productSelect(data.id);
					});
					$("#thisFormula").on("select2:select",function(e) {
						var data = e.params.data;
						if(""== data.id) return;
						prepareDetail(null,data.id);//主动选肯定有id
					});
					$("table").on("change", "[my-sort]", function () {
						mySort();
					});
					function mySort(){
						var indexArr=[
								-1,
								1,//小料件
								1,//绞龙
								1//地磅
						];
						var $allSort = $("[my-sort]").filter(function () {
							return $(this).attr("my-sort") > t_size_1;
						});
						$.each($allSort,function (i, item) {
							var weighPlace=$(item).val();
							$(item).closest("td").next().find(".form-control").val(indexArr[weighPlace]++);
						})
					}
					//stProductBillDetailDataGridAddRowBtn
				</script>
				<h4 class="form-unit">${text('生产工单批号')}</h4>
				<div class="ml10 mr10">
					<table id="stProductBillBatchDetailDataGrid"></table>
					<% if (hasPermi('st:stProductBill:edit')){ %>
						<a href="#" id="stProductBillBatchDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10" style="display: none"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>

			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
<!--						<% if (hasPermi('st:stProductBill:edit')){ %>-->
<!--							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;-->
<!--						<% } %>-->
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>

<script>
//初始化生产工单批号DataGrid对象
$("#stProductBillBatchDetailDataGrid").dataGrid({

	data: ${toJson(stProductBill.stProductBillBatchDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("批号")}', name:'batchNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control',"readonly":true}},
		{header:'${text("小料间状态")}', name:'smallMaterialStatus', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control digits',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('st_product_batch_status')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_smallMaterialStatus";
					$(element).attr("disabled",true);
					$(element).after("<input name='smallMaterialStatus' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("地磅室状态")}', name:'groundStatus', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control digits',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('st_product_batch_status')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_groundStatus";
					$(element).attr("disabled",true);
					$(element).after("<input name='groundStatus' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("绞龙状态")}', name:'packingAugerStatus', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control digits',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('st_product_batch_status')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue',dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_packingAugerStatus";
					$(element).attr("disabled",true);
					$(element).after("<input name='packingAugerStatus' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("父id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				// actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stProductBillBatchDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				// actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stProductBillBatchDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],

	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stProductBillBatchDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stProductBillBatchDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,batchNumber,smallMaterialStatus,groundStatus,packingAugerStatus,parentId.id,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
//初始化生产工单明细DataGrid对象
$("#stProductBillDetailDataGrid").dataGrid({

	data: ${toJson(stProductBill.stProductBillDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("物料名称")}', name:'materialName', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{materialName:'&nbsp;',id:''}], ${materialRecordList}),
				itemLabel: 'materialName', itemValue: 'id', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					element.name="ignore_materialName";
					$(element).attr("disabled",true);
					$(element).after("<input name='materialName' material-id='' value='"+element.value+"' hidden>");
				}
			}
		},
		{header:'${text("占比")}', name:'percentIn', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number percent',"readonly":true}},
		{header:'${text("重量KG")}', name:'weight', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number weight',"readonly":true,dataInit: function(element){
					var index = $(element).closest("tr").find("td").first().html();
					$(element).attr("my-index",index);
				}
			}
		},
		{header:'${text("生产类型")}', name:'productType', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'8', 'class':'form-control',"readonly":true}},
		{header:'${text("称重点")}', name:'weighPlace', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control digits',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('st_weigh_place')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
					var index = $(element).closest("tr").find("td").first().html();
					$(element).attr("my-sort",index);
					// console.info(index);
				}
			}
		},
		{header:'${text("称重顺序")}', name:'weighOrder', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'3', 'class':'form-control digits',"readonly":true}},
		{header:'${text("现有库存量")}', name:'existStockNumber', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number',"readonly":true}},
		{header:'${text("父 id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				// actions.push('<a href="#"  class="detail-del" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stProductBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				actions.push('<a href="#" del-status="0" class="detail-del" onclick="$(\'#stProductBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\');return false;"></a>&nbsp;');
			}else{
				// actions.push('<a href="#" class="detail-del" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stProductBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				actions.push('<a href="#" class="detail-del" onclick="$(\'#stProductBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');return false;"></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],

	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stProductBillDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stProductBillDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,materialName,percentIn,weight,productType,weighPlace,weighOrder,existStockNumber,parentId.id,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			var id = data.data.id;
			refresh(id);
			// $("#id").val(data.data.id);
			// approveBtnStatus(data.data.approveState);
			js.showMessage(data.message);
			// if(data.result == Global.TRUE){
			// 	js.closeCurrentTabPage(function(contentWindow){
			// 		contentWindow.page();
			// 	});
			// }
		}, "json");
    }
});
</script>